package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWithdrawCheckEmp;
import com.zb.mapper.ZjWithdrawCheckEmpMapper;
import com.zb.service.ZjWithdrawCheckEmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 提现审核人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-25
 */
@Service
public class ZjWithdrawCheckEmpServiceImpl extends ServiceImpl<ZjWithdrawCheckEmpMapper, ZjWithdrawCheckEmp> implements ZjWithdrawCheckEmpService {
    @Resource
    private ZjWithdrawCheckEmpMapper withdrawCheckEmpMapper;

    // 保存审核人员
    @Override
    public void save(Long custAccId, List<Long> empIds) {
        deleteByCustAccId(custAccId);
        if (CollectionUtil.isEmpty(empIds)) {
            return;
        }
        List<ZjWithdrawCheckEmp> checkEmpList = new ArrayList<>();
        ZjWithdrawCheckEmp emp = null;
        for (Long empId : empIds) {
            emp = new ZjWithdrawCheckEmp();
            emp.setId(IdWorker.getId());
            emp.setCustAccId(custAccId);
            emp.setEmpId(empId);
            checkEmpList.add(emp);
        }
        this.saveBatch(checkEmpList);
    }

    // 根据商家ID删除审核人员
    @Override
    public void deleteByCustAccId(Long custAccId) {
        QueryWrapper<ZjWithdrawCheckEmp> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        this.remove(wrapper);
    }

    // 根据商家ID获取审核人员
    @Override
    public List<Long> getEmpIdByCustAccId(Long custAccId) {
        QueryWrapper<ZjWithdrawCheckEmp> wrapper = new QueryWrapper<>();
        wrapper.select("emp_id")
                .eq("cust_acc_id", custAccId);
        return this.list(wrapper).stream().map(ZjWithdrawCheckEmp::getEmpId).toList();
    }

    // 根据审核人员ID判断是否有权限审核提现单
    @Override
    public Integer hasCheckAuthorByCustAccIdAndEmpId(Long custAccId, Long empId) {
        return withdrawCheckEmpMapper.hasCheckAuthorByCustAccIdAndEmpId(custAccId, empId);
    }
}
